package com.sqly.doc.menu.jkglfa;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.view.View;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ihidea.multilinechooselib.MultiLineChooseLayout;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo. Jkglfa;
import com.sqly.doc.util.DateConvert;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.SweetAlertDialogUtil;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.CustomDatePicker;
import com.sqly.doc.view.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import cxy.com.validate.IValidateResult;
import cxy.com.validate.Validate;
import cxy.com.validate.annotation.Index;
import cxy.com.validate.annotation.MaxLength;
import cxy.com.validate.annotation.NotNull;
import cz.msebera.android.httpclient.Header;

public class jkglfa_add extends MyBaseActivity {
    @Index(1)
    @NotNull(msg = "方案建立时间不能为空")
    @Bind(R.id.fajlsj)
    TextView fajlsj;

    @Bind(R.id.mnastj)
    RadioGroup mnastj;

    @Bind(R.id.gxytnbtcl)
    RadioGroup gxytnbtcl;

    @Bind(R.id.mnxycl)
    RadioGroup mnxycl;

    @Bind(R.id.gxygwrqbncl)
    RadioGroup gxygwrqbncl;

    @Bind(R.id.yyjkshfs)
    RadioGroup yyjkshfs;

    @Bind(R.id.ljjksdjs)
    RadioGroup ljjksdjs;

    @Bind(R.id.tnbzlwjmc)
    RadioGroup tnbzlwjmc;

    @Bind(R.id.jyw)
    RadioGroup jyw;

    @Bind(R.id.gzss)
    RadioGroup gzss;

    @Bind(R.id.zyzasfy)
    RadioGroup zyzasfy;

    @Bind(R.id.jyzz)
    RadioGroup jyzz;


    private CustomDatePicker customDatePicker;
    Context con = jkglfa_add.this;
    //标题
    TextView title, uname;
    Jkglfa obj;
    String url = "";
    String signupid = "", signuptime="";
    LinearLayout back;

    @Override
    protected void initUI() {
        setContentView(R.layout.jkglfa_add);
        ButterKnife.bind(this);
        Validate.reg(this);
        signupid = getIntent().getStringExtra("signupid");
        signuptime=getIntent().getStringExtra("signuptime");
        if (getIntent().getStringExtra("type").equals("edit")) {
            obj = ( Jkglfa) getIntent().getExtras().getSerializable("b");
            url = HttpUrl.EditJkglfa;
        } else {
            url = HttpUrl.AddJkglfa;
            obj = new  Jkglfa();
        }
        init();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void init() {
        if (obj != null && !StringUtils.isEmpty(obj.getMake_time())) {
            fajlsj.setText(DateConvert.dateToString(DateConvert.toDate(obj.getMake_time(), DateConvert.YMD), DateConvert.YMD)+"");
        }
        final String minfajlsj = DateConvert.dateToString(DateConvert.toDate(signuptime, DateConvert.YMDHM), DateConvert.YMDHM);
        fajlsj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
                customDatePicker = new CustomDatePicker(con, new CustomDatePicker.ResultHandler() {
                    @Override
                    public void handle(String time) { // 回调接口，获得选中的时间
                        fajlsj.setText(time.split(" ")[0]);
                    }
                }, minfajlsj, DateConvert.getCustomDatePickerEndRq()); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
                customDatePicker.showSpecificTime(false); // 不显示时和分
                customDatePicker.setIsLoop(false); // 不允许循环滚动
                if (obj != null && !StringUtils.isEmpty(obj.getMake_time())) {
                    customDatePicker.show(DateConvert.dateToString(DateConvert.toDate(obj.getMake_time(), DateConvert.YMD), DateConvert.YMD));
                }else {
                    customDatePicker.show(minfajlsj);
                }
            }
        });
        //每年定时体检
        if (obj != null && !StringUtils.isEmpty(obj.getMnastj())) {
            switch (Integer.parseInt(obj.getMnastj())) {
                case 1:
                    mnastj.check(R.id.mnastj_shi);
                    break;
                case 0:
                    mnastj.check(R.id.mnastj_fou);
                    break;
            }
        } else {
            mnastj.check(R.id.mnastj_fou);
        }
        mnastj.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.mnastj_shi == checkedId)
                    obj.setMnastj("1");
                if (R.id.mnastj_fou == checkedId)
                    obj.setMnastj("0");
            }
        });

        //每年高血压、高血糖患者血压血糖测量
        if (obj != null && !StringUtils.isEmpty(obj.getGxytnbtcl())) {
            switch (Integer.parseInt(obj.getGxytnbtcl())) {
                case 1:
                    gxytnbtcl.check(R.id.gxytnbtcl_shi);
                    break;
                case 0:
                    gxytnbtcl.check(R.id.gxytnbtcl_fou);
                    break;
            }
        } else {
            gxytnbtcl.check(R.id.gxytnbtcl_fou);
        }
        gxytnbtcl.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.gxytnbtcl_shi == checkedId)
                    obj.setGxytnbtcl("1");
                if (R.id.gxytnbtcl_fou == checkedId)
                    obj.setGxytnbtcl("0");
            }
        });

        //35岁人群每年至少测量一次血压
        if (obj != null && !StringUtils.isEmpty(obj.getMnxycl())) {
            switch (Integer.parseInt(obj.getMnxycl())) {
                case 1:
                    mnxycl.check(R.id.mnxycl_shi);
                    break;
                case 0:
                    mnxycl.check(R.id.mnxycl_fou);
                    break;
            }
        } else {
            mnxycl.check(R.id.mnxycl_fou);
        }
        mnxycl.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.mnxycl_shi == checkedId)
                    obj.setMnxycl("1");
                if (R.id.mnxycl_fou == checkedId)
                    obj.setMnxycl("0");
            }
        });

        //高血压高危人群每半年测量
        if (obj != null && !StringUtils.isEmpty(obj.getGxygwrqbncl())) {
            switch (Integer.parseInt(obj.getGxygwrqbncl())) {
                case 1:
                    gxygwrqbncl.check(R.id.gxygwrqbncl_shi);
                    break;
                case 0:
                    gxygwrqbncl.check(R.id.gxygwrqbncl_fou);
                    break;
            }
        } else {
            gxygwrqbncl.check(R.id.gxygwrqbncl_fou);
        }
        gxygwrqbncl.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.gxygwrqbncl_shi == checkedId)
                    obj.setGxygwrqbncl("1");
                if (R.id.gxygwrqbncl_fou == checkedId)
                    obj.setGxygwrqbncl("0");
            }
        });

        //要有健康生活方式
        if (obj != null && !StringUtils.isEmpty(obj.getYyjkshfs())) {
            switch (Integer.parseInt(obj.getYyjkshfs())) {
                case 1:
                    yyjkshfs.check(R.id.yyjkshfs_shi);
                    break;
                case 0:
                    yyjkshfs.check(R.id.yyjkshfs_fou);
                    break;
            }
        } else {
            yyjkshfs.check(R.id.yyjkshfs_fou);
        }
        yyjkshfs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.yyjkshfs_shi == checkedId)
                    obj.setYyjkshfs("1");
                if (R.id.yyjkshfs_fou == checkedId)
                    obj.setYyjkshfs("0");
            }
        });

        //牢记健康四大基石
        if (obj != null && !StringUtils.isEmpty(obj.getLjjksdjs())) {
            switch (Integer.parseInt(obj.getLjjksdjs())) {
                case 1:
                    ljjksdjs.check(R.id.ljjksdjs_shi);
                    break;
                case 0:
                    ljjksdjs.check(R.id.ljjksdjs_fou);
                    break;
            }
        } else {
            ljjksdjs.check(R.id.ljjksdjs_fou);
        }
        ljjksdjs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.ljjksdjs_shi == checkedId)
                    obj.setLjjksdjs("1");
                if (R.id.ljjksdjs_fou == checkedId)
                    obj.setLjjksdjs("0");
            }
        });

        //糖尿病治疗五驾马车
        if (obj != null && !StringUtils.isEmpty(obj.getTnbzlwjmc())) {
            switch (Integer.parseInt(obj.getTnbzlwjmc())) {
                case 1:
                    tnbzlwjmc.check(R.id.tnbzlwjmc_shi);
                    break;
                case 0:
                    tnbzlwjmc.check(R.id.tnbzlwjmc_fou);
                    break;
            }
        } else {
            tnbzlwjmc.check(R.id.tnbzlwjmc_fou);
        }
        tnbzlwjmc.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.tnbzlwjmc_shi == checkedId)
                    obj.setTnbzlwjmc("1");
                if (R.id.tnbzlwjmc_fou == checkedId)
                    obj.setTnbzlwjmc("0");
            }
        });

        //减腰围
        if (obj != null && !StringUtils.isEmpty(obj.getJyw())) {
            switch (Integer.parseInt(obj.getJyw())) {
                case 1:
                    jyw.check(R.id.jyw_shi);
                    break;
                case 0:
                    jyw.check(R.id.jyw_fou);
                    break;
            }
        } else {
            jyw.check(R.id.jyw_fou);
        }
        jyw.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.jyw_shi == checkedId)
                    obj.setJyw("1");
                if (R.id.jyw_fou == checkedId)
                    obj.setJyw("0");
            }
        });

        //55岁防骨质疏松
        if (obj != null && !StringUtils.isEmpty(obj.getGzss())) {
            switch (Integer.parseInt(obj.getGzss())) {
                case 1:
                    gzss.check(R.id.gzss_shi);
                    break;
                case 0:
                    gzss.check(R.id.gzss_fou);
                    break;
            }
        } else {
            gzss.check(R.id.gzss_fou);
        }
        gzss.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.gzss_shi == checkedId)
                    obj.setGzss("1");
                if (R.id.gzss_fou == checkedId)
                    obj.setGzss("0");
            }
        });

        //遵医嘱按时服药
        if (obj != null && !StringUtils.isEmpty(obj.getZyzasfy())) {
            switch (Integer.parseInt(obj.getZyzasfy())) {
                case 1:
                    zyzasfy.check(R.id.zyzasfy_shi);
                    break;
                case 0:
                    zyzasfy.check(R.id.zyzasfy_fou);
                    break;
            }
        } else {
            zyzasfy.check(R.id.zyzasfy_fou);
        }
        zyzasfy.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.zyzasfy_shi == checkedId)
                    obj.setZyzasfy("1");
                if (R.id.zyzasfy_fou == checkedId)
                    obj.setZyzasfy("0");
            }
        });

        //建议转诊
        if (obj != null && !StringUtils.isEmpty(obj.getJyzz())) {
            switch (Integer.parseInt(obj.getJyzz())) {
                case 1:
                    jyzz.check(R.id.jyzz_shi);
                    break;
                case 0:
                    jyzz.check(R.id.jyzz_fou);
                    break;
            }
        } else {
            jyzz.check(R.id.jyzz_fou);
        }
        jyzz.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.jyzz_shi == checkedId)
                    obj.setJyzz("1");
                if (R.id.jyzz_fou == checkedId)
                    obj.setJyzz("0");
            }
        });

        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        title.setText("健康管理方案");
        back = (LinearLayout) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        uname.setText("保存");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Validate.check(con, new IValidateResult() {
                    @Override
                    public void onValidateSuccess() {
                        //Toast.makeText(con, "验证成功", Toast.LENGTH_SHORT).show();
                        saveData();
                    }

                    @Override
                    public void onValidateError(String msg, View view) {
                        view.setFocusable(true);
                        //Toast.makeText(con, msg, Toast.LENGTH_SHORT).show();
                        SweetAlertDialogUtil.WarningDialog(con, "提示", msg);
                    }

                    @Override
                    public Animation onValidateErrorAnno() {
//                        return ValidateAnimation.horizontalTranslate();
                        return null;
                    }
                });
            }
        });
    }

    @Override
    protected void initData() {

    }
    @Override
    protected void initListener() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Validate.unreg(this);
    }

    private void saveData(){
        //保存
        if (obj == null)
            obj = new  Jkglfa();

        obj.setMake_time(fajlsj.getText().toString());
        if (NetWorkUtils.isNetworkConnected(con)) {
            RequestParams ps = new RequestParams();
            ps.add("entity.sign_up_id", signupid);
            ps.add("entity.make_time", obj.getMake_time());
            ps.add("entity.mnastj", obj.getMnastj());
            ps.add("entity.gxytnbtcl", obj.getGxytnbtcl());
            ps.add("entity.mnxycl", obj.getMnxycl());
            ps.add("entity.gxygwrqbncl", obj.getGxygwrqbncl());
            ps.add("entity.yyjkshfs", obj.getYyjkshfs());
            ps.add("entity.ljjksdjs", obj.getLjjksdjs());
            ps.add("entity.tnbzlwjmc", obj.getTnbzlwjmc());
            ps.add("entity.jyw", obj.getJyw());
            ps.add("entity.gzss", obj.getGzss());
            ps.add("entity.zyzasfy", obj.getZyzasfy());
            ps.add("entity.jyzz", obj.getJyzz());

            if (getIntent().getStringExtra("type").equals("edit")) {
                ps.add("entity.id", obj.getSpid());
            }
            ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
            ps.add("type", "2");
            UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                    String content = new String(response);
                    RetMsg ret = JSON.parseObject(content, RetMsg.class);
                    if (ret.getCode().equals("200")) {
                        MyToastUtil.ShowToast(con, "成功");
                    }else{
                        MyToastUtil.ShowToast(con, ret.getInfo());
                    }
                    finish();
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                    MyToastUtil.ShowToast(con, "失败");
                }
            });
        }
    }
}
